import {info, login, logout} from "@/api/user"
import storage from "@/util/storage.js"
import {STORAGE_LOGIN_USER} from "@/util/const";

const user = {
    state: {
        userName: "",
        nickName: "",
        token: "",
        perms: [],
        roles: []
    },
    getters: {
        // 这个只是判断store（因为刷新页面store会被清空，所以还需要判断storage）
        isLogin(state) {
            return state.token !== "" && state.userName !== "";
        },
        perms(state){
            return state.perms;
        },
        roles(state){
            return state.roles;
        }
    },
    mutations: {
        SAVE_USERNAME(state, userName) {
            state.userName = userName;
        },
        SAVE_NICKNAME(state, nickName) {
            state.nickName = nickName;
        },
        SAVE_TOKEN(state, token) {
            state.token = token;
        },
        SAVE_PERMS(state, perms) {
            state.perms = perms;
        },
        SAVE_ROLES(state, roles) {
            state.roles = roles;
        }
    },
    actions: {
        // 登录
        LOGIN({commit}, payload) {
            return new Promise((resolve, reject) => {
                if (payload) {
                    login(payload.user).then(res => {

                        // console.log(res.data.message);
                        if (res.status === 200) {
                            commit("SAVE_USERNAME", res.data.data.ydlUser.userName);
                            commit("SAVE_NICKNAME", res.data.data.ydlUser.nickName);
                            commit("SAVE_TOKEN", res.data.data.token);

                            storage.saveSessionObject(STORAGE_LOGIN_USER, res.data.data);

                            resolve(res);
                        }
                    }).catch(e => {
                        reject(e)
                    })
                }
            })

        },
        // 登出
        LOGOUT({commit}) {
            return new Promise((resolve, reject) => {
                logout().then(res => {
                    commit("SAVE_USERNAME", "");
                    commit("SAVE_NICKNAME", "");
                    commit("SAVE_TOKEN", "");
                    commit("SAVE_PERMS", []);
                    commit("SAVE_ROLES", []);
                    storage.remove(STORAGE_LOGIN_USER);
                    resolve(res);
                }).catch(e => {
                    reject(e);
                })
            })
        },
        // 恢复vuex中的token等用户信息
        RECOVER_USER({state}) {
            const loginUser = storage.getSessionObject(STORAGE_LOGIN_USER);
            state.userName = loginUser.ydlUser.userName;
            state.nickName = loginUser.ydlUser.nickName;
            state.token = loginUser.token;
        },
        // 将用户鉴权相关信息存到vuex中【perms、roles】
        GET_INFO({commit}) {
            return new Promise((resolve, reject) => {
                info().then(res => {
                    // console.log(res);
                    if (res.status === 200) {
                        commit("SAVE_PERMS",res.data.data.perms);
                        commit("SAVE_ROLES",res.data.data.roles);
                        resolve(res);
                    }
                }).catch(e => {
                    reject(e)
                })
            })
        }
    }
}

export default user